package mygang.controller.manager;

import javax.servlet.http.HttpServletRequest;
import mygang.controller.AbstractController;
import mygang.model.Role;
import mygang.service.manager.RoleService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 25/02/2013
 */
@Controller
@RequestMapping(AbstractController.MANAGE_ROLE)
public class RoleController extends AbstractManageController<RoleService, Role> {

    private static final Logger log = Logger.getLogger(RoleController.class.toString());

    Role prepareObject(HttpServletRequest request) throws Exception {
        String tempId = request.getParameter("id");
        Long id = tempId!=null && !tempId.isEmpty() ? new Long(request.getParameter("id")) : null;
        String name = request.getParameter("name");
        if(name==null || name.isEmpty()) throw new Exception("Name is not specified");
        boolean isActive = Boolean.parseBoolean(request.getParameter("active"));
        return id==null ? new Role(name, isActive) : new Role(id.longValue(), name, isActive);
    }

    String getLink() {
        return MANAGE_ROLE;
    }
}